package com.educate.device.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.educate.common.web.R;
import com.educate.device.model.bo.DeviceBO;
import com.educate.device.model.bo.DeviceMessageBO;
import com.educate.device.model.bo.DeviceStatusBO;
import com.educate.device.rpc.api.DeviceService;
import lombok.extern.log4j.Log4j2;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@RestController
@RequestMapping("device")
public class DeviceController {

    @Reference(version = "${api.service.version}",
            application = "${dubbo.application.id}",
            url = "${dubbo.application.url}")
    private DeviceService deviceService;

    @PostMapping(value = "/create")
    public R create(@RequestBody DeviceBO bo) {
        deviceService.create(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    public R delete(@RequestBody DeviceBO bo) {
        deviceService.delete(bo);
        return R.ok();
    }

    @PostMapping(value = "/delete/{deviceId}")
    public R delete(@PathVariable String deviceId) {
        deviceService.delete(deviceId);
        return R.ok();
    }

    @PostMapping(value = "/detail/{deviceId}")
    public R<DeviceBO> detail(@PathVariable String deviceId) {
        DeviceBO bo = deviceService.detail(deviceId);
        return R.ok(bo);
    }

    @PostMapping(value = "/detail")
    public R<DeviceBO> detail(@RequestBody DeviceBO bo) {
        DeviceBO detail = deviceService.detail(bo);
        return R.ok(detail);
    }

    @PostMapping(value = "/update")
    public R<DeviceBO> update(@RequestBody DeviceBO bo) {
        deviceService.update(bo);
        return R.ok();
    }

    @PostMapping(value = "/query")
    public R<Page<DeviceBO>> query(@RequestBody DeviceBO bo) {
        Page<DeviceBO> result = deviceService.query(bo);
        return R.ok(result);
    }

    @PostMapping(value = "/list")
    public R<List<DeviceBO>> list(@RequestBody DeviceBO bo) {
        List<DeviceBO> result = deviceService.list(bo);
        return R.ok(result);
    }


    /**
     * 查询操作结果
     *
     * @return
     */
    @PostMapping(value = "/resultOperate")
    public R<DeviceMessageBO> resultOperate(@RequestBody DeviceMessageBO bo) {
        DeviceMessageBO result = deviceService.resultOperate(bo);
        return R.ok(result);
    }

    /**
     * 查询设备状态
     *
     * @return
     */
    @PostMapping(value = "/status")
    public R<DeviceStatusBO> status(@RequestBody DeviceBO bo) {
        DeviceStatusBO result = deviceService.state(bo);
        return R.ok(result);
    }
}
